<?php
$method = $_GET[method];
if(!$method)
{
	echo "ERROR: no method specified";
	exit;
}
$host = "webiptv.kaist.ac.kr";
$path = "/iptvportal/WebServices/GetContInfo.asmx";
$ns = "http://tempuri.org/";
$port = 80;
$param = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n".
         "<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">\n".
         " <soap12:Body>\n".
         "  <$method xmlns=\"$ns\" />\n".
         " </soap12:Body>\n".
         "</soap12:Envelope>\n";
$len = strlen($param);


$fp = fsockopen($host, $port, $errno, $errstr, 30);
if($fp)
{
	fputs($fp, "POST $path HTTP/1.1\n");
	fputs($fp, "Host: $host\n");
	fputs($fp, "Content-Type: application/soap+xml; charset=utf-8\n");
	fputs($fp, "Connection: Close\n");
	fputs($fp, "Content-Length: ".$len."\r\n\r\n");
	fputs($fp, $param);
	
	while(!feof($fp))
		$data = $data.fgets($fp, 4096);
	$data = explode("\r\n\r\n", $data);



	$xml = new DOMDocument;
	$xml->loadXML(stripslashes($data[1]));
	$lines = $xml->getElementsByTagName('Body');

	echo "<?xml version=\"1.0\" encoding=\"euc-kr\"?>";
	foreach($lines as $line)
		echo printElements($line->firstChild);

	fclose($fp);
}else
	echo "ERROR: $errstr ($errno)<br/>\n";
?>

<?php
function printElements($domNode)
{
  if($domNode)
  {
	if( !(strpos($domNode->nodeName, "schema")) )
	{
	    if($domNode->nodeType == XML_ELEMENT_NODE)
	    {
		  print "<".encodeNodeValue($domNode->nodeName, true);
	      if($domNode->hasAttributes())
	      {
	        $attributes = $domNode->attributes;
	        foreach($attributes as $domAttribute)
	        {
			  print encodeNodeValue(" $domAttribute->name", true)."=\"".encodeNodeValue("$domAttribute->value\"" , false);
	        }
	      }
	      print ">";
	      if($domNode->hasChildNodes())
	      {
	        $nextNode = $domNode->firstChild;
	        printElements($nextNode);
			print "<"."/".encodeNodeValue( $domNode->nodeName , true).">";
	      }
	      else
	      {
			print encodeNodeValue("$domNode->nodeValue", false)."</".encodeNodeValue($domNode->nodeName, true).">";

	      } 
	    }else print encodeNodeValue("$domNode->nodeValue", false);
	}    
    $nextNode = $domNode->nextSibling;
    printElements($nextNode);

  }
}
?>

<?php
function encodeNodeValue($nodevalue, $isNodeName)
{
	if($nodevalue)
	{
		if($isNodeName)
			return str_replace(":", "_", iconv("UTF-8", "EUC-KR", $nodevalue) );
		else
			return iconv("UTF-8", "EUC-KR", $nodevalue);
	}
}
?>
